package com.fengwk.cv4j.test;

import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.impl.transforms.TanhDerivative;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.ops.transforms.Transforms;

import com.fengwk.cv4j.factory.ind.INDViewFactory;
import com.fengwk.cv4j.view.View;
import com.fengwk.cv4j.view.ind.INDArrayWrap;

public class Ind {
	
	public static void main(String[] args) {
		INDViewFactory f = new INDViewFactory();
		
		INDArray arr1 = Nd4j.create(new float[][] {
			{1, 2, 3},
			{4, 5, 6}
		});
		
		INDArray arr2 = Nd4j.create(new float[][] {
			{1, 2},
			{4, 5},
			{8, 9}
		});
		
		View<INDArrayWrap> varx1 = f.of(arr1);
		View<INDArrayWrap> varx2 = f.of(arr2);
		
		View<INDArrayWrap> var0 = f.of(0);
		View<INDArrayWrap> var1 = f.of(1);
		
		System.out.println("-----------sigmoid-----------");
		View<INDArrayWrap> sigmoid = f.div(var1, f.add(var1, f.exp(f.sub(var0, varx1))));
		System.out.println(sigmoid.compute().arr());
		System.out.println(sigmoid.gradient(varx1).compute().arr());
		System.out.println("-----------sigmoid by nd4j-----------");
		INDArray nd_sigmoid = Transforms.sigmoid(arr1);
		INDArray nd_dsigmoid = arr1.dup();
		Transforms.sigmoidDerivative(nd_dsigmoid, false);
		System.out.println(nd_sigmoid);
		System.out.println(nd_dsigmoid);
		
		System.out.println("-----------tanh-----------");
		View<INDArrayWrap> exp0 = f.exp(varx1);
		View<INDArrayWrap> exp1 = f.exp(f.sub(var0, varx1));
		View<INDArrayWrap> tanh = f.div(f.sub(exp0, exp1), f.add(exp0, exp1));
		System.out.println(tanh.compute().arr());
		System.out.println(tanh.gradient(varx1).compute().arr());
		System.out.println("-----------tanh by nd4j-----------");
		INDArray nd_tanh = Transforms.tanh(arr1);
		INDArray nd_dtanh = arr1.dup();
		Nd4j.getExecutioner().exec(new TanhDerivative(arr1, nd_dtanh));
		System.out.println(nd_tanh);
		System.out.println(nd_dtanh);
		
		System.out.println("-----------sum-----------");
		View<INDArrayWrap> sum_0 = f.sum(varx1, 0);
		System.out.println(sum_0.compute().arr());
		System.out.println(sum_0.gradient(varx1).compute().arr());
		View<INDArrayWrap> sum_1 = f.sum(varx1, 1);
		System.out.println(sum_1.compute().arr());
		System.out.println(sum_1.gradient(varx1).compute().arr());
		View<INDArrayWrap> sum_0_1 = f.sum(varx1, 0, 1);
		System.out.println(sum_0_1.compute().arr());
		System.out.println(sum_0_1.gradient(varx1).compute().arr());
		System.out.println(arr1.size(1));
		View<INDArrayWrap> sum_12mean = f.div(f.sum(varx1, 1), f.of(arr1.size(1)));
		View<INDArrayWrap> d_sum_12mean = sum_12mean.gradient(varx1);
		System.out.println(sum_12mean.compute().arr());
		System.out.println(d_sum_12mean.compute().arr());
		
		System.out.println("-----------mean-----------");
		View<INDArrayWrap> mean_0 = f.mean(varx1, 0);
		System.out.println(mean_0.compute().arr());
		System.out.println(mean_0.gradient(varx1).compute().arr());
		View<INDArrayWrap> mean_1 = f.mean(varx1, 1);
		System.out.println(mean_1.compute().arr());
		System.out.println(mean_1.gradient(varx1).compute().arr());
		View<INDArrayWrap> mean_0_1 = f.mean(varx1, 0, 1);
		System.out.println(mean_0_1.compute().arr());
		System.out.println(mean_0_1.gradient(varx1).compute().arr());
		
		System.out.println("-----------mmul-----------");
		View<INDArrayWrap> mmul = f.mmul(f.pow(varx1, f.of(3)), varx2);
		View<INDArrayWrap> dmmul = mmul.gradient(varx1);
		System.out.println(mmul.compute().arr());
		System.out.println(dmmul.compute().arr());
		
	}
	
}
